package com.woniu113.junk.site.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.AllInventoryDTO;
import com.woniu113.junk.site.dto.ChildInventoryDTO;
import com.woniu113.junk.site.dto.InventoryDTO;
import com.woniu113.junk.site.form.inventory.WareHouseForm;
import com.woniu113.junk.site.model.Inventory;
import com.woniu113.junk.site.param.inventory.WareHouseParam;
import com.woniu113.junk.site.service.InventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.StreamInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  库存控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/inventory")
@Api(value = "InventoryController", tags = "库存信息接口")
public class InventoryController {
    @Resource
    private InventoryService inventoryService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 子站点废品入库
     * @param form 要入库的信息
     * @param token 当前登录用户的token(用于获取当前登录用户的id)
     * @return 返回成功提示和code
     * @throws Exception
     */
    @PostMapping("/wareHouse")
    @ApiOperation(value = "子站点废品入库")
    public Result wareHouse(@RequestBody WareHouseForm form, @RequestHeader String token) throws Exception{
        System.out.println("是否异常"+form.getIsError());
        System.out.println(form.getOrderNum());
        System.out.println(form.getLossReason());
        System.out.println("废品数据"+form.getWasteList());
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        WareHouseParam param = BeanUtil.toBean(form, WareHouseParam.class);
        inventoryService.wareHouse(param,adminId);
        return Result.getResult().setMsg("入库成功").setCode(200);
    }

    @GetMapping("/fWareHouse")
    @ApiOperation(value = "区站点废品入库")
    public Result fWareHouse(@RequestParam("transferId") Integer transferId, @RequestHeader String token) throws Exception{
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        inventoryService.fWareHouse(transferId,adminId);
        return Result.getResult().setMsg("入库成功").setCode(200);
    }

    @GetMapping("/findInventoryByStationId")
    @ApiOperation(value = "查询当前站点的所有废品库存")
    public Result<List<InventoryDTO>> findInventoryByStationId(@RequestParam("stationId") Integer stationId){
        List<InventoryDTO> inventoryList = inventoryService.findInventoryByStationId(stationId);
        return Result.getListResult(InventoryDTO.class).setData(inventoryList).setMsg("查询当前站点的所有废品库存成功").setCode(200);
    }

    @GetMapping("/findAllChildInventoryByCountyStationId")
    @ApiOperation(value = "区站点查询所有子站点的废品库存")
    public Result<List<ChildInventoryDTO>> findAllChildInventoryByCountyStationId(@RequestParam("stationId") Integer stationId){
        List<ChildInventoryDTO> childInventoryDTOList = inventoryService.findAllChildInventoryByCountyStationId(stationId);
        return Result.getListResult(ChildInventoryDTO.class).setData(childInventoryDTOList).setMsg("区站点查询所有子站点的废品库存成功").setCode(200);
    }

    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有站点的库存信息")
    public Result<List<InventoryDTO>> findAll(@RequestParam("stationName") String stationName,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        // 计算查询的起始索引
        AllInventoryDTO inventoryDTO= inventoryService.findAll(stationName,pageNum,pageSize);
        return Result.getListResult(InventoryDTO.class).setData(inventoryDTO.getList()).setMsg("查询所有用户信息成功").setCode(200).setTotal(inventoryDTO.getTotal());
    }
}

